package com.gupao.vip.nio.buffer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 直接缓冲区－加快IO速度
 * @创建人 majingang
 * @联系方式 aoyouxingkong08@163.com
 * @创建日期 2020-04-15
 * @描述
 */
public class DirectBuffer {
    public static void main(String[] args) throws Exception{
        FileInputStream fileInputStream = new FileInputStream("c:\\LibAntiPrtSc_ERROR.log");
        FileChannel fileInputChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("d:\\LibAntiPrtSc_ERROR-copy.log");
        FileChannel fileOutChannel = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(1024);
        while(true){
            byteBuffer.clear();
            int i = fileInputChannel.read(byteBuffer);
            if(i == -1){
                break;
            }
            byteBuffer.flip();
            fileOutChannel.write(byteBuffer);
        }
    }
}
